<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Userprofile extends CI_Controller {

	public function index()
	{	
		 //Userprofile page
		 $this->load->model('transaction');
		 //if(isset($_COOKIE['tiangge_mobtel'])) $mobtel = $_COOKIE['tiangge_mobtel'];
		 //$mobtel = ($mobtel!="") ? urldecode($mobtel) : "";
		 //$userProfile = $this->transaction->retrieveUserProfileToDB($mobtel);
		//$subsId = (isset($_COOKIE['tiangge_subsId'])) ? $_COOKIE['tiangge_subsId'] : "";
		
	   $subsId = $_COOKIE['tiangge_id']; 
	   $ids = array();
	   $subsId2 = explode('.', $subsId);
	   foreach($subsId2 as $value){
		array_push($ids,$value);
	   }
	   $subsId=$ids[2];	
	   
		if($subsId!='') {
		   $subsId = $_COOKIE['tiangge_id']; 
		   $ids = array();
		   $subsId2 = explode('.', $subsId);
		   foreach($subsId2 as $value){
			array_push($ids,$value);
		   }
		   $subsId=$ids[2];
		} else {
		    
			// get cookie mobtel. check if exists, if true, set cookie with subsId. else ""; 
			if(isset($_COOKIE['tiangge_mobtel']) && $_COOKIE['tiangge_mobtel'] != "0") {
				$mobtel = $_COOKIE['tiangge_mobtel'];
				// retrieve subsid using mobtel
				//$subsId = $this->transaction->retrieveSubsId($mobtel);
				// create subsId cookie
				//$exp = intval($_COOKIE['tiangge_expiration'])/1000; 
				
				$id = $_COOKIE['tiangge_id'];
				$idArray = array();
				$id = explode('.', $id);
				$i = 0;
				foreach($id as $value){
				if ($i==2) $value=$this->transaction->retrieveSubsId($mobtel);
				array_push($idArray,$value);
				$i++;
				}
				
				$subsId=implode(".", $idArray);
				$this->input->set_cookie(array('name'=>'tiangge_id', 'value'=>$subsId, 'expire'=>0, 'path'=>'/'));
				//$this->input->set_cookie(array('name'=>'tiangge_subsId', 'value'=>$subsId, 'expire'=>0, 'path'=>'/'));
				/*
				$agent = $_SERVER['HTTP_USER_AGENT'];
				if(strstr($agent,'IMM76D')){
				  setcookie("subsId", $subsId, 0, "/"); 
				}
				else{
				  $exp = intval($_COOKIE['tiangge_expiration'])/1000;	
				  setcookie("subsId", $subsId, $exp + 1797, "/"); 		
				}*/
			} else {
				$subsId = "";
			}
		}
		
		//$subsId = ($subsId!="") ? urldecode($subsId) : "";
		$userProfile = $this->transaction->retrieveUserProfileToDB($subsId);
		 //echo $subsId;
		 //var_dump($userProfile);
		 $d['userinfo'] = $userProfile;		 
		 $views['_content'] = "userprofile";
		 $this->template->load('template_landing', $views, $d);
	}	
	
}
